﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace PGE.Models.Repositorios
{
    public class RepositoryFactory : IRepositorioFabrica
    {
        private PGEDBContext context = new PGEDBContext();

        private Dictionary<Type, object> repositories = new Dictionary<Type, object>();

        public IRepositorio<TEntity> Repository<TEntity>() where TEntity : class
        {
            Type entityType = typeof(TEntity);
            if (!repositories.ContainsKey(entityType))
            {
                IRepositorio<TEntity> newRepository = new BaseRepository<TEntity>(context);
                repositories.Add(entityType, newRepository);
            }
            return (IRepositorio<TEntity>) repositories[entityType];
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public void Dispose()
        {
            context.Dispose();
        }
    }
}